<?php

class Application_Model_StrankaMapper
{

    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Stranka');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Stranka $stranka)
    {
        $data = array(
            'emso'     => $stranka->getEmso(),
            'ime'      => $stranka->getIme(),
            'priimek'  => $stranka->getPriimek(),
            'geslo'    => $stranka->getGeslo(),
            'naslov'   => $stranka->getNaslov(),
            'telefon'  => $stranka->getTelefon(),
            'email'    => $stranka->getEmail()
        );

        try {
            $stranka = new Application_Model_Stranka();
            $this->find($data['emso'], $stranka);
            if ($stranka->getEmso()) {
                $where = $this->getDbTable()->getAdapter()->quoteInto('emso = ?', $data['emso']);
                $this->getDbTable()->update($data, $where);
            } else {
                $this->getDbTable()->insert($data);
            }
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
            die;
        }

    }

    public function find($id, Application_Model_Stranka &$stranka)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $stranka->setEmso($row->emso)
                ->setIme($row->ime)
                ->setPriimek($row->priimek)
                ->setGeslo($row->geslo)
                ->setEmail($row->email)
                ->setTelefon($row->telefon)
                ->setAktiviran($row->aktiviran)
                ->setNaslov($row->naslov)
                ->setToken($row->token);
    }

    public function fetchRow($where) {
        return $this->getDbTable()->fetchRow($where);
    }

    public function authenticate($emso, $geslo) {
        $stranka = new Application_Model_Stranka();

        $this->find($emso, $stranka);

        if (password_verify($geslo, $stranka->getGeslo()) && $stranka->getAktiviran()) {
            return $stranka;
        } else {
            return null;
        }
    }

    public function getAllStranke($where = null) {
        $strankeArray = $this->fetchAll($where);
        $result = array();
        foreach ($strankeArray as $stranka) {
            $strankaModel = new Application_Model_Stranka();
            $strankaModel->setEmso($stranka['emso']);
            $strankaModel->setIme($stranka['ime']);
            $strankaModel->setPriimek($stranka['priimek']);
            $strankaModel->setAktiviran($stranka['aktiviran']);
            $strankaModel->setEmail($stranka['email']);
            $strankaModel->setTelefon($stranka['telefon']);
            $strankaModel->setNaslov($stranka['naslov']);
            $result[$stranka['emso']] = $strankaModel;
        }
        return $result;
    }

    public function fetchAll($where) {
        return $this->getDbTable()->fetchAll($where);
    }

    public function update($emso, $polje, $vrednost) {
        $where = $this->getDbTable()->getAdapter()->quoteInto('emso = ?', $emso);
        $this->getDbTable()->update(array($polje => $vrednost), $where);
    }

    public function delete($id)
    {
        $where = $this->getDbTable()->getAdapter()->quoteInto('emso = ?', $id);
        return $this->getDbTable()->delete($where);
    }
}

